package cn.sdc.bugKiller.service;

import java.util.List;

import cn.sdc.bugKiller.bean.BugBean;
import cn.sdc.bugKiller.bean.BugCategroyBean;
import cn.sdc.bugKiller.bean.BugDealBean;
import cn.sdc.bugKiller.bean.BugProcessBean;
import cn.sdc.bugKiller.bean.BugSearchBean;
import cn.sdc.bugKiller.bean.BugStatus;
import cn.sdc.bugKiller.bean.CodeBean;
import cn.sdc.bugKiller.bean.MemberBean;
import cn.sdc.bugKiller.bean.ModuleBean;
import cn.sdc.bugKiller.bean.PriorityBean;
import cn.sdc.bugKiller.bean.ProgramBean;
import cn.sdc.bugKiller.bean.ProjectBean;
import cn.sdc.bugKiller.dao.BugDaoImpl;
import cn.sdc.bugKiller.dao.BugProcessDaoImpl;
import cn.sdc.bugKiller.dao.CodeDaoImpl;
import cn.sdc.bugKiller.dao.MemberDaoImpl;

public class BugDealService {

	private static BugDaoImpl bugDao = new BugDaoImpl();
	private static MemberDaoImpl memberDao = new MemberDaoImpl();
	private static CodeDaoImpl codeDao = new CodeDaoImpl();
	private static BugProcessDaoImpl processDao = new BugProcessDaoImpl();
	
	public BugBean insertBug(BugBean bean){
		BugBean inBean = null;
		if(bean != null){
			inBean = bugDao.insertBug(bean);
			BugProcessBean firstProcessBean = inBean.createFirstProcess();
			firstProcessBean = processDao.insertBean(firstProcessBean);
			if(firstProcessBean != null){
				bugDao.commit();
				return inBean;
			}
		}
		bugDao.rollback();
		return inBean;
	}
	
	public int deleteBugById(Integer bugId){
		int result = 0;
		if(bugId != null){
			result = bugDao.delBugById(bugId);
			if(result > 0){
				result = processDao.delAllProcessByBugId(bugId);
			}
			if(result > 0){
				bugDao.commit();
				return result;
			}
		}
		bugDao.rollback();
		return result;
	}
	
	public BugDealBean updateBugProcess(BugDealBean bean) {
		if(bean==null || bean.getId()==null){
			return null;
		}
		BugBean bugBean = bugDao.getBugById(bean.getId());
		if(bugBean != null){
			bean.setBug(bugBean);
			BugProcessBean nextProcessBean = bean.createNextProcess();
			nextProcessBean = processDao.insertBean(nextProcessBean);
			
			if(nextProcessBean != null){
				bugBean.setStatus(bean.getNextBugStatus());
				bugBean = bugDao.updateBug(bugBean);
				
				if(bugBean!=null ){
					bugDao.commit();
					return getBugDealBeanByBugId(bean.getId());
				}
			}
		}
		bugDao.rollback();
		return null;
	}
	
	public BugBean updateBug(BugBean bean) {
		return updateBug(bean, false);
	}

	public BugBean updateBug(BugBean bean, boolean reassignFlg) {
		BugBean bugBean = null;
		if(bean != null){
			bugBean = bugDao.updateBug(bean);
			if(bugBean!=null && reassignFlg){
				BugProcessBean firstProcessBean = bugBean.createReassignProcess();
				firstProcessBean = processDao.insertBean(firstProcessBean);
				if(firstProcessBean != null){
					bugDao.commit();
					return bugBean;
				}
			}
		}
		bugDao.rollback();
		return bugBean;
	}

	public BugDealBean forceClosedBugProcess(BugDealBean bean) {
		if(bean==null || bean.getId()==null){
			return null;
		}
		BugBean bugBean = bugDao.getBugById(bean.getId());
		if(bugBean != null){
			bean.setBug(bugBean);
			BugProcessBean nextProcessBean = bean.createForceCloseProcess();
			nextProcessBean = processDao.insertBean(nextProcessBean);
			
			if(nextProcessBean != null){
				bugBean.setStatus(BugStatus.STATUS_CLOSED);
				bugBean = bugDao.updateBug(bugBean);
				
				if(bugBean!=null ){
					bugDao.commit();
					return getBugDealBeanByBugId(bean.getId());
				}
			}
		}
		bugDao.rollback();
		return null;
	}
	
	public Long countUnfixBug(){
		return bugDao.countBugByStatus(BugStatus.STATUS_UNFIX);
	}
	
	public Long countApprovalBug(){
		return bugDao.countBugByStatus(BugStatus.STATUS_APPROVAL);
	}
	
	public Long countClosedBug(){
		return bugDao.countBugByStatus(BugStatus.STATUS_CLOSED);
	}
	
	public List<BugBean> searchUnfixBugList(){
		return bugDao.searchBugListByStatus(BugStatus.STATUS_UNFIX);
	}
	
	public List<BugBean> searchApprovalBugList(){
		return bugDao.searchBugListByStatus(BugStatus.STATUS_APPROVAL);
	}
	
	public List<BugBean> searchClosedBugList(){
		return bugDao.searchBugListByStatus(BugStatus.STATUS_CLOSED);
	}
	
	public Long countAllBug(){
		return bugDao.countBugByStatus();
	}
	
	public BugBean getBugBeanById(Integer id){
		return bugDao.getBugById(id);
	}
	
	public List<BugBean> getUrgentlyBugList(){
		return bugDao.searchStatusBugListByPriority(BugStatus.STATUS_UNFIX,
				new Integer(8),PriorityBean.PRIORITY_3,PriorityBean.PRIORITY_4, PriorityBean.PRIORITY_5);
	}
	
	public List<BugBean> getNewlyBugList(){
		return bugDao.searchNewlyBugList(new Integer(8));
	}
	
	public List<BugBean> searchBugList(BugSearchBean searchBean){
		return bugDao.searchBug(searchBean);
	}
	
	//DealBug about bugProcess
	public List<BugProcessBean> getAllProcessByBugId(Integer bugId){
		return processDao.getAllProcessByBugId(bugId);
	}
	
	public BugDealBean getBugDealBeanByBugId(Integer bugId){
		
		BugDealBean dealBean = null;
		
		if(bugId == null){
			return null;
		}
		
		BugBean bugBean = null;
		List<BugProcessBean> processList = null;
		
		//get bug
		bugBean = getBugBeanById(bugId);
		if(bugBean == null){
			return null;
		}
		dealBean = new BugDealBean();
		dealBean.setBug(bugBean);
		dealBean.setId(bugBean.getId());

		//get bug processList
		processList = getAllProcessByBugId(bugId);
		if(processList != null
				&& bugBean.getStatus() != null
				&& !bugBean.getStatus().equals(BugStatus.STATUS_CLOSED)){
			
			processList.add(dealBean.createBlankNextProcess());
		}
		dealBean.setProcessList(processList);
		
		//set role
		Integer currentUserId = ContextService.getCurrentUserId();
		if(bugBean.getAssigner()!=null && bugBean.getAssigner().equals(currentUserId)){
			dealBean.setAssignerFlg(true);
		}
		if(bugBean.getHandler()!=null && bugBean.getHandler().equals(currentUserId)){
			dealBean.setHandlerFlg(true);
		}
		
		return dealBean;
	}
	
	
	/**
	 * setBeanProperty
	 * Delete by lijunqiang On 20140221
	 * change to get BeanProperty with sql
	 * @param bean
	 * @return
	 */
	@Deprecated
	public BugBean setBeanProperty(BugBean bean){
		if(bean==null){
			return null;
		}
		
		if(bean.getModule() != null){
			ModuleBean moduleBean = (ModuleBean)codeDao.getCodeById(bean.getModule(), ModuleBean.TBL_NAME,ModuleBean.class);
			if(moduleBean != null){
				bean.setModuleBean(moduleBean);
			}
		}
		
		if(bean.getProgram() != null){
			ProgramBean programBean = (ProgramBean)codeDao.getCodeById(bean.getProgram(), ProgramBean.TBL_NAME,ProgramBean.class);
			if(programBean != null){
				bean.setProgramBean(programBean);
			}
		}
		
		if(bean.getBugCategroy() != null){
			BugCategroyBean bugCategroyBean = (BugCategroyBean)codeDao.getCodeById(bean.getBugCategroy(), BugCategroyBean.TBL_NAME,BugCategroyBean.class);
			if(bugCategroyBean != null){
				bean.setBugCategroyBean(bugCategroyBean);
			}
		}
		
		if(bean.getPriorityBean() != null){
			PriorityBean priorityBean = (PriorityBean)codeDao.getCodeById(bean.getPriority(), PriorityBean.TBL_NAME,PriorityBean.class);
			if(priorityBean != null){
				bean.setPriorityBean(priorityBean);
			}
		}
		
		if(bean.getAssigner()!=null){
			MemberBean assignerBean = memberDao.getMemberById(bean.getAssigner());
			if(assignerBean != null){
				bean.setAssignerBean(assignerBean);
			}
		}
		
		if(bean.getHandler()!=null){
			MemberBean handlerBean = memberDao.getMemberById(bean.getHandler());
			if(handlerBean != null){
				bean.setHandlerBean(handlerBean);
			}
		}
		
		return bean;
	}
}
